﻿namespace FlickrDownloadr.Controls
{
	using System.ComponentModel;
	using System.Windows;
	using System.Windows.Controls;

	public class IconButton : Button, INotifyPropertyChanged
	{
		public IconButton() : base()
		{
			this.DefaultStyleKey = typeof(IconButton);
		}

        protected override void OnInitialized(System.EventArgs e)
        {
            base.OnInitialized(e);

            //var test = GetTemplateChild("Background");

            //FrameworkElement icon = GetTemplateChild(Icon.ToString() + "Icon") as FrameworkElement;
            //icon.Visibility = System.Windows.Visibility.Visible;
        }

        protected override void OnTemplateChanged(ControlTemplate oldTemplate, ControlTemplate newTemplate)
        {
            base.OnTemplateChanged(oldTemplate, newTemplate);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }

        protected override void OnClick()
        {
            base.OnClick();

            var test = GetTemplateChild("Background");
            FrameworkElement icon = GetTemplateChild(Icon.ToString() + "Icon") as FrameworkElement;
        }

        protected override void OnVisualParentChanged(DependencyObject oldParent)
        {
            base.OnVisualParentChanged(oldParent);

            //var test = GetTemplateChild("Background");

            //FrameworkElement icon = GetTemplateChild(Icon.ToString() + "Icon") as FrameworkElement;
            //icon.Visibility = System.Windows.Visibility.Visible;
        }

        //public override void OnApplyTemplate()
        //{
        //    base.OnApplyTemplate();

        //    FrameworkElement icon = GetTemplateChild(Icon.ToString() + "Icon") as FrameworkElement;
        //    icon.Visibility = System.Windows.Visibility.Visible;

        //    //Canvas iconContainer = GetTemplateChild("IconContainer") as Canvas;

        //    //if (iconContainer != null)
        //    //{
        //    //    var icon = Application.Current.Resources[Icon.ToString() + "Icon"] as FrameworkElement;
        //    //    iconContainer.Children.Add(icon);
        //    //}
        //}

		public Icons Icon
		{
			get { return (Icons)GetValue(IconProperty); }
			set { SetValue(IconProperty, value); }
		}

		private static void OnIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			IconButton iconButton = d as IconButton;

			if (iconButton == null) return;

			OnPropertyChanged(iconButton, "Icon");
		}

		private static void OnPropertyChanged(IconButton button, string propertyName)
		{
			if (button.PropertyChanged != null)
			{
				button.PropertyChanged(button, new PropertyChangedEventArgs(propertyName));
			}
		}

		public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(Icons), typeof(IconButton), new PropertyMetadata(OnIconChanged));
	
		public event PropertyChangedEventHandler  PropertyChanged;
	}

	public enum Icons
	{
		Plus,
		Minus,
        Left,
        MoveLeft,
        MoveRight
	}
}
